package com.langsin.shihao.mapper;

import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.shihao.mapper.GoodsMapper;
import com.langsin.shihao.mapper.TeacherMapper;
import com.langsin.shihao.mapper.UserMapper;
import com.langsin.shihao.mapper.WorkerMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyTest {
    SqlSession sqlSession=null;
    //每次单元测试类之前执行
    @Before
    public void before() throws Exception{
        //读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //获取一个会话（连接）默认不提交
         sqlSession = sqlSessionFactory.openSession();
    }
    //每次单元测试类之后执行
    @After
    public void after() throws Exception{
        sqlSession.commit();
        sqlSession.close();
    }
   @Test
   public void test() throws Exception {
       UserMapper mapper = sqlSession.getMapper(UserMapper.class);
       User user = mapper.queryByID(702);
       System.out.println(user);
   }
    @Test
    public void test1() throws Exception {
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<Integer> ids=new ArrayList<>();
        ids.add(9003);
        ids.add(9008);
        ids.add(9009);
        List<Goods> goods = mapper.queryByIds(ids);
        for (Goods good : goods) {
            System.out.println(good);
        }
    }
    @Test
    public void test2() throws Exception {
       Goods goods=new Goods();
       goods.setGoodsName("%C%");
       goods.setShopPrice(18.0);
       goods.setGoodsStock(900);
        GoodsMapper mapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goodsList = mapper.queryByCondition(goods);
        for (Goods goods1 : goodsList) {
            System.out.println(goods1);
        }

    }
    @Test
    public void test3() throws Exception {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserOrders(702);
        System.out.println(user);
        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders);
        }
    }
    @Test
    public void test4() throws Exception {
        Worker worker=new Worker();
        worker.setWid("1540932087520215040");
        worker.setWname("大律师");
        worker.setAge(45);
        worker.setSex("男");
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.updateWorkerByCondition(worker);
        System.out.println(row);

    }
    @Test
    public void test5() throws Exception {
        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
        Orders orders = mapper.queryOdersAndUserByOdersId(1002);
       //先获取主表的信息
        System.out.println(orders.getOrderId()+"--"+orders.getOrderDate());
        //模拟过了一段时间后
        try {
            Thread.sleep(10000);
        }catch (Exception e){
            e.printStackTrace();
        }
        //获取关联用户的信息
        System.out.println(orders.getUser());


    }
    //这个是多对多的情况
    @Test
    public void test6() throws Exception {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserGoods(702);
        System.out.println(user);
        System.out.println("========---------");
        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders);

            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail);
            }
        }

    }
    @Test
    public void test7() throws Exception {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserOrders(704);
        System.out.println(user.getCname() + "," + user.getAddress());
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Orders orders : user.getOrdersList()) {
            System.out.println(orders);
        }
    }


}
